package com.example.startapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.startapp.R;
import com.example.startapp.entity.User;
import com.example.startapp.util.DBOpenHelper;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 登录验证
 */

public class LoginActivity extends AppCompatActivity {

    private static final String TYPE_LOGIN = "登录";
    private DBOpenHelper mDBOpenHelper;
    @BindView(R.id.btn_login)
    TextView btn_login;//登录按钮
    @BindView(R.id.btn_register)
    TextView btn_register;//注册按钮
    @BindView(R.id.ed_name)
    EditText ed_name;//用户名
    @BindView(R.id.ed_pd)
    EditText ed_pd;//密码
    @BindView(R.id.back)
    ImageView back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        ButterKnife.bind(this);
        init();
        mDBOpenHelper = new DBOpenHelper(this);


    }

    private void init() {
        //密码暗文
        ed_pd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
        ed_pd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        /**
         * 登录跳转
         */
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                /*
                验证信息
                 */
                String name = ed_name.getText().toString();//获取键入的用户名
                String password = ed_pd.getText().toString();//获取键入的密码
                if (checkLogin(name, password)) {
                    mDBOpenHelper.add(name,password,TYPE_LOGIN);
                    startActivity(intent);

                }

                finish();
            }
        });

        /**
         * 注册跳转
         */
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();

            }
        });
        /**
         * 返回
         */
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_back=new Intent(LoginActivity.this,MainActivity.class);
                intent_back.putExtra("id",4);
                startActivity(intent_back);

            }
        });


    }

    /**
     * 确认是否存在用户，密码是否正确
     * @param name
     * @param password
     * @return
     */
    public boolean checkLogin(String name, String password) {
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
            ArrayList<User> data = mDBOpenHelper.getAllData();
            boolean match = false;
            for (int i = 0; i < data.size(); i++) {
                User user = data.get(i);
                if (name.equals(user.getName()) && password.equals(user.getPassword())) {
                    match = true;
                    break;
                } else {
                    match = false;
                }

            }
            if (match) {
                Toast.makeText(this, "successful!", Toast.LENGTH_SHORT).show();
                return true;
            } else {
                Toast.makeText(this, "fail!", Toast.LENGTH_SHORT).show();
                return false;
            }
        } else {
            Toast.makeText(this, "不能为空!", Toast.LENGTH_SHORT).show();

            return false;
        }

    }


}
